<template>

    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades">Bom清单</i>
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="输入物料名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <!--        增加记录        -->
                <el-button type="primary" icon="el-icon-zoom-in" @click="handleAdd">添加产品</el-button>
                <el-button icon="el-icon-download" @click="exportExcel">导出</el-button>

            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    id="out-table"
                    header-cell-class-name="table-header"
                    row-key="id"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>

                <el-table-column type="index" width="55" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" align="center" label="物料" width="250"></el-table-column>
                <el-table-column prop="num" align="center" label="数量" width="80" ></el-table-column>
                <el-table-column prop="content" align="center" label="描述" ></el-table-column>
                <el-table-column label="操作"  align="center" width="200">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-folder-add"
                                @click="handleBomChild(scope.$index, scope.row)"
                        >添加</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>

            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                        align="center"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="95px">
                <el-form-item label="物料"><el-input v-model="form.name"></el-input></el-form-item>
                <el-form-item label="数量" ><el-input v-model="form.num"></el-input></el-form-item>
                <el-form-item label="描述" ><el-input v-model="form.content"></el-input></el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 添加弹出框 -->
        <el-dialog title="添加" :visible.sync="addVisible" width="30%">
            <el-form ref="form" :model="form" label-width="95px">
                <el-form-item label="物料"><el-input v-model="form.name"></el-input></el-form-item>
                <el-form-item label="数量" ><el-input v-model="form.num"></el-input></el-form-item>
                <el-form-item label="描述" ><el-input v-model="form.content"></el-input></el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import FileSaver from 'file-saver'
    import XLSX from 'xlsx'
    export default {
        name: "bom",
        data() {
            return {
                query: {
                    name:'',
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                addVisible: false,
                editVisible: false,
                pageTotal: 0,
                form: {},
                idx: -1,

            };

        },
        created() {
            this.getData();
        },
        methods: {

            //获取数据
            getData() {
                this.$axios.get('/api/product/bom').then(res =>{
                    if (res.data.code!=='200') {
                        this.$message.error(res.data.message);

                    }else {
                        this.pageSplit(res.data.data);

                    }

                });
            },
            // 编辑操作
            handleEdit(index, row) {

                this.idx = index;
                this.form = row;
                this.editVisible = true;
            },
            // 删除操作
            handleDelete(row) {
                // 二次确认删除
                this.$confirm('你将删除该物料及其子目录', '提示', {
                    type: 'warning'
                })
                    .then(() => {

                        this.$axios.get('/api/product/deleteBom?id='+row.id).then(res=>{
                            if(res.data.code==="200"){
                                this.$message.success(res.data.message);
                                this.getData();
                            }else {
                                this.$message.error(res.data.message);
                            }

                        })
                    })
                    .catch(() => {});
            },
            // 保存编辑
            saveEdit() {

                this.editVisible = false;
                this.$axios.post('/api/product/editBom',this.form).then(res=>{
                    if(res.data.code==="200"){
                        this.$message.success(res.data.message);
                        this.getData();
                    }else {
                        this.$message.error(res.data.message);
                    }

                });
                this.$set(this.tableData, this.idx, this.form);
            },
            // 保存添加
            saveAdd() {
                console.log(this.form);
                this.addVisible = false;
                this.$axios.post('/api/product/bomAdd',this.form).then(res=>{
                    if(res.data.code==='200'){
                        this.$message.success(res.data.message);
                        this.getData();
                    }else {
                        this.$message.error(res.data.message);
                    }
                    this.form={};
                });

            },
            // 触发搜索按钮
            handleSearch() {
                this.$axios.get('/api/product/searchBom?name='+this.query.name).then(res =>{
                    if (res.data.code!=='200') {
                        this.$message.error(res.data.message);
                    }else {
                        this.pageSplit(res.data.data);
                    }
                });

            },
            //分页
            pageSplit(data){
                this.pageTotal=data.length;
                let start= (this.query.pageIndex-1)*this.query.pageSize;
                let end = this.query.pageIndex*this.query.pageSize<this.pageTotal?this.query.pageIndex*this.query.pageSize:this.pageTotal;
                this.tableData=[];
                for (let i=start;i<end;i++){
                    this.tableData[i-start]=data[i];
                }
            },

            //添加操作
            handleAdd() {
                this.addVisible = true;
            },//添加操作
            handleBomChild(index, row) {
                this.addVisible = true;
                this.form.fatherId = row.id;
            },
            exportExcel () {
                /* out-table关联导出的dom节点  */
                var wb = XLSX.utils.table_to_book(document.querySelector('#out-table'));
                /* get binary string as output */
                var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
                try {
                    FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '产品列表.xlsx')
                } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
                return wbout
            },
            // 分页导航
            handlePageChange(val) {

                this.$set(this.query, 'pageIndex', val);
                this.getData();
            }

        }
    }
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>